home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / packages / icomplete.el < prev    next >
Encoding:
Text File  |  1995-08-22  |  10.9 KB  |  264 lines

  1. ;;; icomplete.el --- minibuffer completion with incremental feedback
  2.  
  3. ;;; Loading this package implements a more finely-grained completion
  4. ;;; feedback scheme, indicating prospective minibuffer completion
  5. ;;; candidates as you type.  See the documentation string for
  6. ;;; 'icomplete-prompt' for a specific description of icompletion.
  7.  
  8. ;;; This will not work on Emacs 18 versions - there may be a version
  9. ;;; for v18 in the elisp archives, at archive.cis.ohio-state.edu, in
  10. ;;; /pub/gnu/emacs/elisp-archive.
  11.  
  12. ;;; It should run on most version of Emacs 19 (including XEmacs
  13. ;;; 19 - thanks to the efforts of Colin Rafferty (craffert@lehman.com)
  14. ;;; - thanks, Colin!)
  15.  
  16. ;;;_ + Provide
  17. (provide 'icomplete)
  18.  
  19. ;;;_ + Package Identification Stuff
  20. ;; Author: Ken Manheimer <klm@nist.gov>
  21. ;; Maintainer: Ken Manheimer <klm@nist.gov>
  22. ;; Version: <<<cvs ate me>>>
  23. ;; Created: Mar 1993 klm@nist.gov - first release to usenet
  24. ;; Keywords: help, abbrev
  25. ;;;_  - LCD Archive Entry and RCS ID
  26. ;;; icomplete|Ken Manheimer|klm@nist.gov
  27. ;;; |Completion enhancement, with incremental feedback in the minibuffer.
  28. ;;;
  29.  
  30. ;;;_  - GNU Copyright
  31. ;;; This program is free software; you can redistribute it and/or modify
  32. ;;; it under the terms of the GNU General Public License as published by
  33. ;;; the Free Software Foundation; either version 1, or (at your option)
  34. ;;; any later version.
  35.  
  36. ;;; This program is distributed in the hope that it will be useful,
  37. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  38. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  39. ;;; GNU General Public License for more details.
  40.  
  41. ;;; You should have received a copy of the GNU General Public License
  42. ;;; along with this program; if not, write to the Free Software
  43. ;;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  44.  
  45. ;;; Synched up with: Not synched with FSF.
  46.  
  47. ;;; Developed by Ken Manheimer, klm@nist.gov.
  48.  
  49. ;;;_ + User Customization variables
  50. ;;;_  = icomplete-inhibit
  51. (defvar icomplete-inhibit nil
  52.   "*Set this variable to t at any time to inhibit icomplete.")
  53.  
  54. ;;;_ + Setup
  55. ;;;_  - Internal Variables
  56. ;;;_   = icomplete-eoinput 1
  57. (defvar icomplete-eoinput 1
  58.   "Point where minibuffer input ends and completion info begins.")
  59. (make-variable-buffer-local 'icomplete-eoinput)
  60. ;;;_  > icomplete-prime-session ()
  61. (defun icomplete-prime-session ()
  62.  
  63.   "Prep emacs v 19 for icompletion.  For emacs v19.18 and later revs,
  64. icomplete is installed in 'minibuffer-setup-hook'.  Global pre- and
  65. post-command-hook functions are used in v19.17 and earlier revs."
  66.  
  67.   (cond ((string-match "^19\\.\\([0-9]+\\)\\." emacs-version)
  68.      (let* ((vers (string-to-int (substring emacs-version
  69.                         (match-beginning 1)
  70.                         (match-end 1)))))
  71.        (if (> vers 17)
  72.            ;; Post v19rev17, use minibuffer-setup-hook:
  73.            (add-hook 'minibuffer-setup-hook 'icomplete-prime-minibuffer)
  74.          ;; v19rev18 and prior, use global pre- and -post-commands:
  75.          (add-hook 'pre-command-hook 'icomplete-pre-command-hook 'append)
  76.          (add-hook 'post-command-hook
  77.                'icomplete-post-command-hook 'append))))
  78.     ((string-match "XEmacs" emacs-version)
  79.      (add-hook 'pre-command-hook 'icomplete-pre-command-hook 'append)
  80.      (add-hook 'post-command-hook
  81.            'icomplete-post-command-hook 'append))
  82.     (format "icomplete.el: non v19 emacs / XEmacs version %s - %s"
  83.         emacs-version "try prior icomplete code")))
  84. ;;;_  > icomplete-prime-minibuffer ()
  85. (defun icomplete-prime-minibuffer ()
  86.  
  87.   "Prep emacs, v 19.18 or later, for icomplete.  \(icomplete-prime-
  88. session establishes global hooks, instead, in emacs 19 versions 19.17
  89. and prior.\)  Run via minibuffer-setup-hook \(emacs 19.18 or later\),
  90. adds icomplete pre- and post-command hooks to do icomplete display
  91. management."
  92.  
  93.   ;; We append the hooks because preliminary version of blink-paren
  94.   ;; post-command-hook i have interferes with proper operation of
  95.   ;; minibuffer quit.
  96.   (add-hook 'local-pre-command-hook 'icomplete-pre-command-hook)
  97.   (add-hook 'local-post-command-hook 'icomplete-post-command-hook))
  98.  
  99. ;;;_  > icomplete-window-minibuffer-p ()
  100. (defun icomplete-window-minibuffer-p ()
  101.  
  102.   "Returns non-nil if current window is a minibuffer window."
  103.  
  104.   (if (boundp 'xemacs-logo)
  105.       (window-minibuffer-p nil)
  106.     (window-minibuffer-p)
  107.     ))
  108.  
  109. ;;;_ + Completion
  110. ;;;_  - Completion feedback hooks
  111. ;;;_   > icomplete-pre-command-hook ()
  112. (defun icomplete-pre-command-hook ()
  113.   "Cleanup completions exhibit before user's new input (or whatever) is dealt
  114. with."
  115.   (if (and (icomplete-window-minibuffer-p)
  116.        (not (symbolp minibuffer-completion-table))
  117.        (not icomplete-inhibit))
  118.       (if (and (boundp 'icomplete-eoinput)
  119.            icomplete-eoinput)
  120.       (if (> icomplete-eoinput (point-max))
  121.           ;; Oops, got rug pulled out from under us - reinit:
  122.           (setq icomplete-eoinput (point-max))
  123.         (let ((buffer-undo-list buffer-undo-list ))    ; prevent entry
  124.           (delete-region icomplete-eoinput (point-max))))
  125.     (make-local-variable 'icomplete-eoinput)
  126.     (setq icomplete-eoinput 1))))
  127. ;;;_   > icomplete-post-command-hook ()
  128. (defun icomplete-post-command-hook ()
  129.   "Exhibit completions, leaving icomplete-eoinput with position where user
  130. input leaves off and exhibit begins, so icomplete-pre-command-hook can
  131. subsequently cleanup."
  132.   (if (and (icomplete-window-minibuffer-p)    ; ... in a minibuffer.
  133.        (not icomplete-inhibit)    ; ... not specifically inhibited.
  134.        ;(sit-for 0)            ; ... redisplay and if there's input
  135.                     ; waiting, then don't icomplete
  136.                     ; (stigs suggestion)
  137.        ;; Inhibit for file-name and other custom-func completions:
  138.        (not (symbolp minibuffer-completion-table))
  139.        )
  140.       (let ((buffer-undo-list buffer-undo-list ))    ; prevent entry
  141.     (icomplete-exhibit))))
  142. ;;;_   > icomplete-window-setup-hook ()
  143. (defun icomplete-window-setup-hook ()
  144.   "Exhibit completions, leaving icomplete-eoinput with position where user
  145. input leaves off and exhibit begins, so icomplete-pre-command-hook can
  146. subsequently cleanup."
  147.   (if (and (icomplete-window-minibuffer-p)    ; ... in a minibuffer.
  148.        )
  149.       (message "ic ws doing")(sit-for 1)))
  150. ;;;_   > icomplete-exhibit ()
  151. (defun icomplete-exhibit ()
  152.   "Exhibit completions, leaving icomplete-eoinput with position where user
  153. input leaves off and exhibit begins, so icomplete-pre-command-hook can
  154. subsequently cleanup."
  155.   (if (not (symbolp minibuffer-completion-table))
  156.       (let ((contents (buffer-substring (point-min)(point-max)))
  157.         (buffer-undo-list t))
  158.     (save-excursion
  159.       (goto-char (point-max))
  160.                                         ; Register the end of input, so we
  161.                                         ; know where the extra stuff
  162.                                         ; (match-status info) begins:
  163.       (if (not (boundp 'icomplete-eoinput))
  164.           ;; In case it got wiped out by major mode business:
  165.           (make-local-variable 'icomplete-eoinput))
  166.       (setq icomplete-eoinput (point))
  167.                                         ; Insert the match-status information:
  168.       (if (> (point-max) 1)
  169.           (insert-string
  170.         (icomplete-prompt contents
  171.                   minibuffer-completion-table
  172.                   minibuffer-completion-predicate
  173.                   (not
  174.                    minibuffer-completion-confirm))))))))
  175.  
  176. ;;;_  - Completion feedback producer
  177. ;;;_   > icomplete-prompt (name candidates predicate require-match)
  178. (defun icomplete-prompt (name candidates predicate require-match)
  179.   "Identify prospective candidates for minibuffer completion.
  180.  
  181. The display is updated with each minibuffer keystroke when icomplete
  182. is enabled \(by loading the 'icomplete' elisp package\) and doing
  183. minibuffer completion.
  184.  
  185. Prospective completion suffixes (if any) are displayed, bracketed by
  186. one of \(), \[], or \{} pairs.  The choice of brackets is as follows:
  187.  
  188.   \(...) - a single prospect is identified and matching is enforced,
  189.   \[...] - a single prospect is identified but matching is optional, or
  190.   \{...} - multiple prospects, separated by commas, are indicated, and
  191.           further input is required to distingish a single one.
  192.  
  193. The displays for disambiguous matches have \" [Matched]\" appended
  194. \(whether complete or not), or \" \[No matches]\", if no eligible
  195. matches exist."
  196.  
  197.   (let ((comps (all-completions name candidates predicate))
  198.                                         ; "-determined" - only one candidate
  199.         (open-bracket-determined (if require-match "(" "["))
  200.         (close-bracket-determined (if require-match ")" "]"))
  201.                                         ;"-prospects" - more than one candidate
  202.         (open-bracket-prospects "{")
  203.         (close-bracket-prospects "}")
  204.         )
  205.     (cond ((null comps) (format " %sNo matches%s"
  206.                                 open-bracket-determined
  207.                                 close-bracket-determined))
  208.           ((null (cdr comps))           ;one match
  209.            (concat (if (and (> (length (car comps))
  210.                                (length name)))
  211.                        (concat open-bracket-determined
  212.                                (substring (car comps) (length name))
  213.                                close-bracket-determined)
  214.                      "")
  215.                    " [Matched]"))
  216.           (t                            ;multiple matches
  217.            (let* ((most (try-completion name candidates predicate))
  218.                   (most-len (length most))
  219.                   most-is-exact
  220.                   (alternatives
  221.                    (apply
  222.                     'concat
  223.                     (cdr (apply 'append
  224.                                 (mapcar '(lambda (com)
  225.                                            (if (= (length com) most-len)
  226.                                                ;; Most is one exact match,
  227.                                                ;; note that and leave out
  228.                                                ;; for later indication:
  229.                                                (progn
  230.                                                  (setq most-is-exact t)
  231.                                                  ())
  232.                                              (list ","
  233.                                                    (substring com
  234.                                                               most-len))))
  235.                                         comps))))))
  236.              (concat (and (> most-len (length name))
  237.                           (concat open-bracket-determined
  238.                                   (substring most (length name))
  239.                                   close-bracket-determined))
  240.                      open-bracket-prospects
  241.                      (if most-is-exact
  242.                          (concat "," alternatives)
  243.                        alternatives)
  244.                      close-bracket-prospects))))))
  245.  
  246. ;;;_ + Initialization
  247. (icomplete-prime-session)
  248.  
  249. ;;;_* Local emacs vars.
  250. '(
  251. Local variables:
  252. eval: (save-excursion
  253.         (if (not (condition-case err (outline-mode t)
  254.                    (wrong-number-of-arguments nil)))
  255.             (progn
  256.               (message
  257.                "Allout outline-mode not loaded, not adjusting buffer exposure")
  258.               (sit-for 1))
  259.           (message "Adjusting '%s' visibility" (buffer-name))
  260.           (outline-lead-with-comment-string ";;;_")
  261.           (goto-char 0)
  262.           (outline-exposure -1 0)))
  263. End:)
  264.